package minisoft.application.loterryservice;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;

public class UserDataSource {

	private static final String SEPARATOR = ";";
	private User user = new User();
	private Context context;
	public UserDataSource() {
	}
	public UserDataSource(Context context) {
		this.context = context;
		this.setUser();
	}
	
	public void setRegistration(Context context, String reg) {
		SharedPreferences sharedPref = context.getSharedPreferences(
		        context.getString(R.string.preference_file_key), Context.MODE_PRIVATE);
		SharedPreferences.Editor prefEditor = sharedPref.edit(); // Get
																	// preference
																	// in editor
																	// mode
		prefEditor.putString("registrationId", reg); // set your default value
														// here (could be empty
														// as well)
		prefEditor.commit(); // finally save changes
	}
	
	public void setDevice(Context context, String reg) {
		SharedPreferences sharedPref = PreferenceManager
				.getDefaultSharedPreferences(context);
		SharedPreferences.Editor prefEditor = sharedPref.edit(); // Get
																	// preference
																	// in editor
																	// mode
		prefEditor.putString("device", reg); // set your default value
														// here (could be empty
														// as well)
		prefEditor.commit(); // finally save changes
	}	

	public void setUser() {
		//SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
		SharedPreferences sharedPrefs = context.getSharedPreferences(
				context.getPackageName() + "_preferences", Context.MODE_PRIVATE);
		
		String region = sharedPrefs.getString("region", ""); 
		Log.i("region name", region);
		user.setRegion(region);

		user.setMessageMode(sharedPrefs.getString("message_mode", ""));
		user.setRegistration(sharedPrefs.getString("registrationId", ""));
		user.setDevice(sharedPrefs.getString("device", ""));
		String profile_message = sharedPrefs.getString("profile_message", "");

		if (profile_message != null && (!profile_message.equals(""))) {
			String[] st = profile_message.split(SEPARATOR);
			String[] array_profile = context.getResources().getStringArray(
					R.array.profile_values);

			for (int i = 0; i < st.length; ++i) {
				if (st[i].equals(array_profile[0])) {
					user.setRingMode(true);
				}

				if (st[i].equals(array_profile[1])) {
					user.setVibrateMode(true);
				}
			}

		} else {
			user.setVibrateMode(false);
			user.setRingMode(false);
		}
	}
	
	public int getRegionId()
	{
		return Integer.parseInt(user.getRegion());
/*		String[] region_array = context.getResources().getStringArray(R.array.message_region_mode_value);
		String region = user.getRegion();
		for(int i=0; i< region_array.length; ++i)
		{
			if(region_array[i].equals(region))
			{
				return i;
			}
		}
		return 1;	
*/	}
}